import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/show")
public class ShowRequestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //返回 URL  方法   版本号  query string   context path
        StringBuilder sb = new StringBuilder();
        sb.append(req.getMethod());
        sb.append("<br>");
        sb.append(req.getRequestURI());
        sb.append("<br>");
        sb.append(req.getProtocol());
        sb.append("<br>");
        sb.append(req.getContextPath());
        sb.append("<br>");
        sb.append(req.getQueryString());
        sb.append("<br>");

        // 返回所有的 header
        Enumeration<String> enumeration = req.getHeaderNames();
        while(enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = req.getHeader(key);
            sb.append(key + " : " + value + "<br>");

        }

        // 对浏览器声明返回响应的类型,并将响应返回客户端
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write(sb.toString());
    }
}
